import { endpoint, shareImage } from '../../utils/util.js';
var app = getApp();

Component({
  data: {
    dailyFreeParseNum: '--',
    totalParseNum: '--',
    hasUserInfo: false,
    userInfo: {},
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onLoad: function () {
      console.log('onLoad')
      console.log(wx.getStorageSync('userInfo'))
      if(wx.getStorageSync('userInfo')){
        this.setData({
          userInfo: wx.getStorageSync('userInfo'),
          hasUserInfo: true,
        })
      }
    },
    onShow: function () {
      console.log('onShow')
      console.log(app.checkIsLogin())
      if (!app.checkIsLogin()) {
        console.info("未登录")
        this.getUserInfo()
      }else{
        console.info("已登录")
        var userInfo = wx.getStorageSync('userInfo');
        if (userInfo.nickName !== '微信用户') {
          this.updateUserInfo(userInfo)
        }
        // 获取每日剩余免费解析次数
        this.getDailyFreeParseNum();
        // 获取当前用户总解析次数
        this.getTotalParseNum();
      }
    },

    updateUserInfo(userInfo){
      app.apiRequest({
        url: endpoint.getUser,
        success: res => {
          console.info("用户信息")
          console.info(wx.getStorageSync('userInfo'))
          wx.setStorageSync('userInfo', userInfo)
          this.setData({
            userInfo: wx.getStorageSync('userInfo'),
            hasUserInfo: true,
          })
        }
      })
    },
    /**
     * 授权登录
     */
    getUserInfo: function() {
      app.getUserInfo(res => {
        this.setData({
          userInfo: wx.getStorageSync('userInfo'),
          hasUserInfo: true,
        })
        this.updateUserInfo(wx.getStorageSync('userInfo'))
        wx.hideLoading();
        var that = this;
        setTimeout(function () {
          // 获取每日剩余免费解析次数
          that.getDailyFreeParseNum();
          // 获取当前用户总解析次数
          that.getTotalParseNum();
        }, 1000)
      })
    },

    /**
     * 获取当日免费次数
     * 使用本地存储，不走服务端
     */
    getDailyFreeParseNum() {
        app.apiRequest({
          url: endpoint.getFreeNum,
          success: res => {
            console.log('getFreeNum')
            console.log(res)
            wx.setStorageSync('dailyFreeParseNum', res.data);
            this.setData({
              dailyFreeParseNum: res.data
            })
          }
        })
    },

    /**
     * 获取总解析次数
     */
    getTotalParseNum() {
      app.apiRequest({
        url: endpoint.getTotalParseNum,
        success: res => {
          this.setData({
            totalParseNum: res.data.total_num
          })
        }
      })
    },

    //打赏
    showQrcode() {
      wx.previewImage({
        urls: ['https://wechat.larwas.com/pay.jpg'],
        current: 'https://wechat.larwas.com/pay.jpg' // 当前显示图片的http链接
      })
    },
    toUserInfo(){
      if(this.data.hasUserInfo){
        wx.navigateTo({
          url: '/pages/mine/userInfo',
        })
      }
    },
    //分享小程序
    onShareAppMessage: function () {
      return {
        title: '推荐一款超好用的视频去水印工具，免费解析不限次，大家都在用',
        path: '/pages/index/index',
        imageUrl: shareImage
      }
    },
    loginSuccess: function(e) {
      console.log(e.detail.code) // wx.login 的 code
      console.log(e.detail.userInfo) // wx.getUserInfo 的 userInfo
    }
  }
})